package com.example.cart.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.cart.R;
import com.example.cart.application.CustomApplication;
import com.example.cart.database.ShoppingDatabase;
import com.example.cart.entity.Product;

import java.util.List;

/**
 * 商品主页
 */
public class ShoppingIndexActivity extends AppCompatActivity {

    private ShoppingDatabase shoppingDatabase;

    private TextView tvCount;

    private ImageView ivCart;

    private GridLayout glProductList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_shopping_index);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        shoppingDatabase = CustomApplication.getInstance().getShoppingDatabase();

        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("商品列表");

        tvCount = findViewById(R.id.tv_count);
        glProductList = findViewById(R.id.gl_product_list);

        ivCart = findViewById(R.id.iv_cart);
        ivCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到购物车界面
                Intent intent = new Intent();
                intent.setClass(ShoppingIndexActivity.this, CartIndexActivity.class);
                startActivity(intent);
            }
        });

        showProducts();
    }

    private void showProducts() {
        // 获取屏幕的宽度
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        // 设置单个商品展示时的Layout参数
        LinearLayout.LayoutParams productLayoutParams = new LinearLayout.LayoutParams(
                screenWidth/2, LinearLayout.LayoutParams.WRAP_CONTENT);

        List<Product> productList = shoppingDatabase.productDao().queryAll();
        for (Product product : productList) {
            // 根据布局文件product_item.xml实例化一个布局视图对象
            View vProductItem = getLayoutInflater().inflate(R.layout.product_item, null);
            // 设置商品名称
            TextView tvProductName = vProductItem.findViewById(R.id.tv_product_name);
            tvProductName.setText(product.getName());
            // 设置商品展示的图片
            ImageView ivProductPic = vProductItem.findViewById(R.id.iv_product_pic);
            ivProductPic.setImageURI(Uri.parse(product.getPicPath()));
            // 设置价格
            TextView tvPrice = vProductItem.findViewById(R.id.tv_price);
            tvPrice.setText(String.valueOf(product.getPrice()));
            // 设置按钮点击事件
            Button btnAddCart = vProductItem.findViewById(R.id.btn_add_cart);
            btnAddCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    shoppingDatabase.addProductToCart(product);
                    // 增加购物车的数量展示
                    String originCount = tvCount.getText().toString();
                    if (!originCount.isEmpty()) {
                        tvCount.setText(String.valueOf(Integer.parseInt(originCount) + 1));
                    } else {
                        tvCount.setText(String.valueOf(1));
                    }
                }
            });

            // 将商品实图添加到商品列表Layout中
            glProductList.addView(vProductItem, productLayoutParams);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 刷新购物车商品数量
        int totalQuantity = shoppingDatabase.cartDao().queryTotalQuantity();
        tvCount.setText(String.valueOf(totalQuantity));
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
    }
}